<template>
  <div class="fg1 y oh h40px">
    <div class="y p10px fg1 oh">
      <el-table height="100" :data="tabData2" border stripe highlight-current-row class="fg1">
        <el-table-column fixed type="index" label="行号" width="50" />
        <el-table-column fixed prop="id" label="id" width="50" />
        <el-table-column prop="projectName" label="项目" width="100" />
        <el-table-column prop="project" label="列次" min-width="200">
          <template slot-scope="scope">
            <div v-if="typeof scope.row.project=='string'">{{scope.row.project}}</div>
            <sProject0
              v-else
              :disabled="fun<1"
              :value="scope.row.project"
              :list="project"
              @change="change('project',scope.$index,$event)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="process" label="工序" width="120">
          <template slot-scope="scope">
            <sProcess
              :disabled="fun<1 || !scope.row.project.length"
              :list="scope.row.processList"
              :value="scope.row.process"
              @change="change('process',scope.$index,$event);"
            />
          </template>
        </el-table-column>
        <el-table-column prop="uid" label="物料号" width="180">
          <template slot-scope="scope">
            <sItemUid
              :disabled="fun<1 || !scope.row.process"
              :list="scope.row.itemList"
              v-model="scope.row.uid"
              @change="change('item',scope.$index,$event)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="name" label="名称" width="180" />
        <el-table-column prop="model" label="型号" width="180" />
        <el-table-column prop="sum" label="数量" width="100">
          <template slot-scope="scope">
            <el-input
              :value="scope.row.sum"
              @input="proving(scope.$index,$event)"
              @focus="$event.currentTarget.select();"
              :disabled="fun<1 || !scope.row.depository"
              :class="scope.row.sum>0?'Blue':'Danger'"
            />
          </template>
        </el-table-column>
        <el-table-column prop="depository" label="仓库" min-width="150">
          <template slot-scope="scope">
            <sDepository
              :disabled="fun<1 || !scope.row.id"
              v-model="scope.row.depository"
              @change="change('depository',scope.$index,$event)"
            />
          </template>
        </el-table-column>
        <template v-if="fun">
          <el-table-column
            prop="stock.current"
            label="当前仓库"
            width="80"
            show-overflow-tooltip
            header-align="center"
            align="center"
          />
          <el-table-column
            prop="stock.sum"
            label="需求"
            width="80"
            show-overflow-tooltip
            header-align="center"
            align="center"
          />
          <el-table-column
            prop="stock.arrived"
            label="已到"
            width="80"
            show-overflow-tooltip
            header-align="center"
            align="center"
          />
          <el-table-column
            prop="stock.use"
            label="已用"
            width="80"
            show-overflow-tooltip
            header-align="center"
            align="center"
          />
          <el-table-column
            prop="stock.stock"
            label="总库存"
            width="80"
            show-overflow-tooltip
            header-align="center"
            align="center"
          />
          <el-table-column prop="remarks" label="操作" width="80">
            <template slot-scope="scope">
              <el-button
                v-if="fun>0"
                @click="remove(scope.$index)"
                v-text="'移除'"
                type="primary"
                size="mini"
              />
              <el-button
                v-else
                @click="$emit('change','change',scope.$index)"
                v-text="'修改'"
                type="success"
                size="mini"
              />
            </template>
          </el-table-column>
        </template>
        <el-table-column prop="remarks" label="备注">
          <template slot-scope="scope">
            <el-input :disabled="fun<1" v-model="scope.row.remarks" />
          </template>
        </el-table-column>
      </el-table>
      <div class="wf x zb" style="padding-top:10px" v-if="buttons">
        <div class="x jc" v-if="buttons.left">
          <el-button
            v-for="i in buttons.left"
            :key="i.id"
            :class="i.class"
            v-text="i.name"
            @click="$emit('change',i.id)"
            :style="i.style?i.style:''"
            :size="i.Attributes.size"
            :type="i.Attributes.type?i.Attributes.type:'primary'"
            :plain="i.Attributes.plain?true:false"
            :rount="i.Attributes.rount?true:false"
            :circle="i.Attributes.circle?true:false"
            :loading="i.Attributes.loading?true:false"
            :disabled="i.Attributes.disabled?true:false"
            :icon="i.Attributes.icon?i.Attributes.icon:''"
            :autofocus="i.Attributes.autofocus?true:false"
          />
        </div>
        <div class="wf x jc ze" v-if="buttons.right">
          <el-button
            v-for="i in buttons.right"
            :key="i.id"
            :class="i.class"
            v-text="i.name"
            @click="$emit('change',i.id)"
            :style="i.style?i.style:''"
            :size="i.Attributes.size"
            :type="i.Attributes.type?i.Attributes.type:'primary'"
            :plain="i.Attributes.plain?true:false"
            :rount="i.Attributes.rount?true:false"
            :circle="i.Attributes.circle?true:false"
            :loading="i.Attributes.loading?true:false"
            :disabled="i.Attributes.disabled?true:false"
            :icon="i.Attributes.icon?i.Attributes.icon:''"
            :autofocus="i.Attributes.autofocus?true:false"
          />
        </div>
      </div>
    </div>
    <fy :value="tabData" @change="tabData2=$event" />
  </div>
</template>
<script>
import sItemUid from "../../components/select/sItemUid";
import sProject0 from "../../components/select/sProject0";
import sDepository from "../../components/select/sDepository";
import sProcess from "../../components/select/sProcess";
import fy from "../../components/fy";
export default {
  components: { sItemUid, sDepository, sProcess, sProject0, fy },
  // props: ["value", "buttons", "fun"],
  props: {
    value: { default: [] },
    buttons: { default: false },
    fun: { default: 0 }
  },
  watch: {
    "$gd.depository"(a) {
      this.tree.depository = a.arr2tree();
    },
    "$gd.project"(a) {
      this.tree.project = a.arr2tree();
    },
    value(a) {
      for (let i = 0; i < a.length; i++) {
        if (
          !this.$isset(a[i].projectName) &&
          this.$isset(a[i].project) &&
          a[i].project.length
        )
          this.porjectChange(i, a[i].project[0]);
      }
      this.tabData = a;
    }
  },
  data() {
    return {
      tree: {
        depository: [],
        project: []
      },
      tabData: [],
      tabData2: [],
      project: this.$gd.project.arr2tree()
    };
  },
  computed: {},
  created() {
    if (this.$gd.depository)
      this.tree.depository = this.$gd.depository.arr2tree();
    if (this.$gd.project) this.tree.project = this.$gd.project.arr2tree();
    if (this.value.length) {
      this.tabData = this.value;
    }
  },
  methods: {
    proving(a, b) {
      this.$emit("change", "sum", a, b);
    },
    porjectChange(a, b) {
      this.tabData[a].projectName = this.tree.project.v2item(b).name;
      // porjectChange
    },
    remove(a) {
      let b = 1;
      if (this.fun == 3) {
        b = 2;
        a = a & (a ^ 1);
      }
      // console.log(a, b);
      let t = this.$cp(this.tabData2[a]);
      a = this.tabData.getItem(t, true);
      this.tabData.splice(a, b);
    },
    change(a, b, c, d) {
      let t = this.$cp(this.tabData2[b]);
      b = this.tabData.getItem(t, true);
      let m = 0;
      let s = 0;
      let tmp = {};
      switch (a) {
        case "item":
          this.tabData[b].id = c.id;
          this.tabData[b].item = c.id;
          this.tabData[b].uid = c.uid;
          this.tabData[b].name = c.name;
          this.tabData[b].model = c.model;
          this.tabData[b].depository = "";
          this.tabData[b].sum = 0;
          tmp = this.$cp(this.tabData[b]);
          tmp.project = tmp.project[tmp.project.length - 1];
          m = 0;
          t = { sum: 0, arrived: 0, use: 0, stock: 0 };
          this.$gd.stock.map((i, j) => {
            if (
              i.project == tmp.project &&
              i.item == tmp.id &&
              i.process == tmp.process
            ) {
              if (i.depository) {
                t.arrived += i.arrived;
                t.use += i.use;
                t.stock += i.stock;
                // m += i.sum - i.arrived;
                s += i.stock;
              } else {
                t.sum += i.sum;
                // m += i.sum - i.arrived;
                // s += i.stock;
              }
              m += i.sum - i.arrived;
            }
          });
          console.log(m, s);
          this.tabData[b].in = m;
          this.tabData[b].out = s;
          this.tabData[b].stock = t;
          break;
        case "depository":
          s = 0;
          if (c) {
            t = this.tabData[b].project;
            if (Array.isArray(t)) t = t[t.length - 1];
            t = {
              depository: c,
              project: t,
              item: this.tabData[b].item,
              process: this.tabData[b].process
            };
            tmp = this.$gd.stock.getItem(t);
            if (tmp) s = tmp.stock;
          }
          this.tabData[b].depository = c;
          this.tabData[b].sum = 0;
          this.tabData[b].stock.current = s;
          break;
        case "project":
          this.tabData[b].projectName = c.length
            ? this.tree.project.getItem(c[0]).name
            : "";
          this.tabData[b].project = c;
          this.tabData[b].process = "";
          this.tabData[b].id = 0;
          this.tabData[b].model = "";
          this.tabData[b].name = "";
          this.tabData[b].sum = 0;
          this.tabData[b].uid = "";
          this.tabData[b].depository = "";
          this.tabData[b].stock = {};
          if (this.fun) {
            tmp = [];
            t = c;
            s = [];
            if (Array.isArray(t)) t = t[t.length - 1];
            this.$gd.stock.map(a => {
              if (!a.depository && a.project == t) {
                if (tmp.indexOf(a.process) < 0) {
                  tmp.push(a.process);
                  s.push(this.$gd.process.getItem({ id: a.process }));
                }
              }
            });
            this.tabData[b].processList = s;
          }
          // console.log(this.$cp(this.tabData[b]));
          break;
        case "process":
          this.tabData[b].process = c;
          this.tabData[b].id = 0;
          this.tabData[b].model = "";
          this.tabData[b].name = "";
          this.tabData[b].uid = "";
          this.tabData[b].depository = "";
          this.tabData[b].sum = 0;
          this.tabData[b].stock = {};
          if (this.fun) {
            t = this.tabData[b].project;
            if (Array.isArray(t)) t = t[t.length - 1];
            tmp = [];
            s = [];
            this.$gd.stock.map(a => {
              if (!a.depository && a.project == t && a.process == c) {
                if (tmp.indexOf(a.item) < 0) {
                  tmp.push(a.item);
                  s.push(this.$gd.items.getItem({ id: a.item }));
                }
              }
            });
            this.tabData[b].itemList = s;
          }
          this.tabData = this.$cp(this.tabData);
          break;

        default:
          break;
      }
      this.$emit("change", "change", this.tabData);
    },
    getProcessByProject(a) {
      let project = a;
      if (Array.isArray(a) && a.length) project = a[a.length - 1];
      project = parseInt(project);
      if (!project) return;
      console.log(project);
      let ret = [];
      this.$gd.stock.map(i => {
        if (
          !i.depository &&
          i.project == project &&
          !ret.getItem({ id: i.process })
        ) {
          ret.push(this.$cp(this.$gd.process.getItem({ id: i.process })));
        }
      });
      return ret;
    },
    getProcessByPP(a, b) {
      let project = a[a.length - 1];
      let ret = [];
      this.$gd.stock.map(i => {
        if (
          !i.depository &&
          i.project == project &&
          i.process == b &&
          !ret.getItem({ id: i.item })
        ) {
          ret.push(this.$cp(this.$gd.items.getItem({ id: i.item })));
        }
      });
      return ret;
    }
  }
};
</script>

<style scoped>
.Blue /deep/ .el-input__inner {
  color: #409eff !important;
}
.Danger /deep/ .el-input__inner {
  color: #f56c6c !important;
}
</style>
